package org.monkey.metrics;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.management.*;
import java.util.ArrayList;
import java.util.List;

public class MemoryMetrics implements Runnable {
    private static Logger logger = LoggerFactory.getLogger(MemoryMetrics.class);
    private OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
    private MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
    private List<MemoryManagerMXBean> memoryManagerMXBeans = ManagementFactory.getMemoryManagerMXBeans();
    private List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans();

    public MemoryMetrics() {
//        System.out.println("memoryManagerMXBeans : " + JSON.toJSONString(memoryManagerMXBeans));
//        System.out.println("memoryPoolMXBeans : " + JSON.toJSONString(memoryPoolMXBeans));
    }

    @Override
    public void run() {
        long percert = 1024 * 1024;


        double systemLoadAverage = operatingSystemMXBean.getSystemLoadAverage();
        System.out.println("systemLoadAverage : " + systemLoadAverage);
        MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
        long committed = heapMemoryUsage.getCommitted() /percert;
        long init = heapMemoryUsage.getInit() /percert;
        long used = heapMemoryUsage.getUsed() /percert;
        long max = heapMemoryUsage.getMax() /percert;


//        System.out.println("heapMemoryUsage : committed : " + committed);
//        System.out.println("heapMemoryUsage : init : " + init);
//        System.out.println("heapMemoryUsage : used : " + used);
//        System.out.println("heapMemoryUsage : max : " + max);


        MemoryUsage nonHeapMemoryUsage = memoryMXBean.getNonHeapMemoryUsage();
        long nonHeapMemorycommitted = nonHeapMemoryUsage.getCommitted() /percert;
        long nonHeapMemoryinit = nonHeapMemoryUsage.getInit() /percert;
        long nonHeapMemoryused = nonHeapMemoryUsage.getUsed() /percert;
        long nonHeapMemorymax = nonHeapMemoryUsage.getMax() /percert;


//        System.out.println("nonHeapMemoryUsage : committed : " + nonHeapMemorycommitted);
//        System.out.println("nonHeapMemoryUsage : init : " + nonHeapMemoryinit);
//        System.out.println("nonHeapMemoryUsage : used : " + nonHeapMemoryused);
//        System.out.println("nonHeapMemoryUsage : max : " + nonHeapMemorymax);


        long totalcommitted = nonHeapMemorycommitted + committed;
        long totalinit = nonHeapMemoryinit + init;
        long totaluserd = nonHeapMemoryused + used;
        long totalmax = nonHeapMemorymax + max;
        long total = totalcommitted + totalinit + totaluserd + totalmax;

        System.out.println("total : committed : " + totalcommitted);
        System.out.println("total : init : " + totalinit);
        System.out.println("total : used : " + totaluserd);
        System.out.println("total : max : " + totalmax);
        System.out.println("total : " + total);




//        int objectPendingFinalizationCount = memoryMXBean.getObjectPendingFinalizationCount();



    }

    
}